<?php
/**
 * Moduleadmin_Model_Moduleadmin
 * module admin main model
 *
 * @author Adrian Stolarski
 */
class Moduleadmin_Model_Moduleadmin implements Moduleadmin_Model_Mainable, 
        Moduleadmin_Model_Decidable {
    
    /**
     *
     * @var type $module_access_level
     * module access level handler
     */
    public $module_access_level;
    /**
     *
     * @var type $action_handler
     * action handler
     */
    public $action_handler;
    public $page_data = array();
    /**
     * constructor()
     */
    public function __construct() {
        $this->module_access_level = new Core_Controller_Moduleaccesslevel;
    }
    
    public function main(array $variables = null) {
        $this->module_access_level->main($variables);
        $page = $this->model_decision($variables);
        //var_dump($page);
        //die();
        return $page;
    }

    public function model_decision(array $variables = null) {
        switch($variables['get']['name']){
            case 'modules_list':
                //$page = $this->modules_list($variables);
                //var_dump($page);
                //die();
                return $this->modules_list($variables);
                break;
            case "add_module":
                $page = $this->add_module($variables);
                //var_dump($page);
                //die();
                return $page;
                break;
            case "insert_module":
                $this->insert_module($variables['post']);
            case 'delete_module':
                $this->delete_module($variables['post']);
                break;
            case "edit_module":
                //var_dump($variables);
                //die();
                return $this->edit_module($variables['post']);
                break;
            case "update_module":
                //var_dump($variables);
                //die();
                $this->update_module($variables['post']);
                break;
            default:
                return $this->modules_list($variables);
                break;
        }
    }
    
    /**
     * update_module()
     * update current module data
     * 
     * @param array $variables
     */
    private function update_module(array $variables = null){
        //var_dump($variables);
        //die();
        if(isset($variables['csrf_token']) && ($variables['csrf_token'] == $_SESSION['csrf_token'])){
            //var_dump($variables);
            //die();
            $this->action_handler = new Moduleadmin_Model_Updatemodule;
            $this->action_handler->main($variables);
        }
        else {
            session_destroy();
            $this->action_handler = new Login_Data_Loginredirect;
            $this->action_handler->main($variables);
        }
    }

    /**
     * 
     * @param array $variables
     * @return type
     */
    private function edit_module(array $variables = null){
        //var_dump($variables);
        //die();
        $this->action_handler = new Moduleadmin_Data_Editmodule;
        return $this->action_handler->main($variables);
    }
    /**
     * insert_module()
     * insert new module
     * 
     * @param array $variables
     */
    private function insert_module(array $variables = null){
        //var_dump($variables);
        //die();
        if(isset($variables['csrf_token']) 
                && ($variables['csrf_token'] == $_SESSION['csrf_token'])
                ){
            //var_dump($variables);
            //die();
            $this->action_handler = new Moduleadmin_Model_Insertmodule;
            $this->action_handler->main($variables);
        }
        else {
            //var_dump($variables);
            //die();
            session_destroy();
            $this->action_handler = new Login_Data_Loginredirect;
            $this->action_handler->main($variables);
        }
    }
    
    /**
     * delete_module()
     * delete module action 
     * 
     * @param array $variables
     */
    private function delete_module(array $variables = null){
        //var_dump($variables);
        //die();
        if($variables['csrf_token'] = $_SESSION['csrf_token']){
            $this->action_handler = new Moduleadmin_Model_Deletemodule;
            $this->action_handler->main($variables);
        }
        else {
            $this->action_handler = new Moduleadmin_Data_Modulelistredirect;
            $this->action_handler->main($variables);
        }
    }
    
    /**
     * add_module()
     * return addmodule template data
     * 
     * @param array $variables
     * @return type $page
     */
    private function add_module(array $variables = null){
        //var_dump($variables);
        //die();
        $this->action_handler = new Moduleadmin_Data_Addmodule;
        $page =  $this->action_handler->main($variables);
        //var_dump($page);
        //die();
        return $page;
    }
    /**
     * module_list()
     * get module list into database 
     * 
     * @param array $variables
     * @return type $page_data
     */
    private function modules_list(array $variables = null){
        //var_dump($variables);
        $this->action_handler = new Moduleadmin_Data_Modulelist;
        $page_data = $this->action_handler->main($variables);
        //var_dump($page_data);
        //die();
        return $this->action_handler->main($variables);
    }

}
